package com.tql.mall.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.tql.mall.common.constant.PageConstant;
import com.tql.mall.merchant.client.MerchantFeignClient;
import com.tql.mall.model.merchant.ShopInfo;
import com.tql.mall.model.search.SearchParam;
import com.tql.mall.model.search.SearchRespVo;
import com.tql.mall.search.client.SearchFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: tyk
 * @Date: 2023/5/24 16:23
 * @Description:
 */
@Controller
public class SearchWebController {

    @Autowired
    private SearchFeignClient searchFeignClient;

    @Autowired
    private MerchantFeignClient merchantFeignClient;

@GetMapping("search.html")
public String search(SearchParam searchParam, Model model) {

    // 店铺关键字清空
    searchParam.setKeywordInShop("");
    searchParam.setShopId(null);

    SearchRespVo resp = searchFeignClient.search(searchParam);
    model.addAllAttributes(
            JSONObject.parseObject(JSONObject.toJSONString(resp))
    );

    // 店铺数据
    Long shopId = searchParam.getShopId();
    if (shopId != null) {
        ShopInfo shop = merchantFeignClient.getShopInfoEntity(shopId);
        model.addAttribute("shop", shop);
    }

    // 搜索条件回显
    model.addAttribute("searchParam", searchParam);

    // 拼接urlParam
    String urlParam = makeUrlParam(searchParam);
    model.addAttribute("urlParam", urlParam);

    // 面包屑——品牌
    String tradeMarkParam = makeUrlTradeMarkParam(searchParam.getTrademark());
    model.addAttribute("tradeMarkParam", tradeMarkParam);

    // 面包屑——平台属性
    List<Map<String, String>> propsParamList = makePropsParamList(searchParam.getProps());
    model.addAttribute("propsParamList", propsParamList);

    // 排序处理
    Map<String, String> orderMap = makeOrderMap(searchParam.getOrder());
    model.addAttribute("orderMap", orderMap);

    return "search/index";
}

    @GetMapping("shop.html")
    public String shop(SearchParam searchParam, Model model) {

        // 主关键字清空
        searchParam.setKeyword("");

        SearchRespVo resp = searchFeignClient.search(searchParam);
        model.addAllAttributes(
                JSONObject.parseObject(JSONObject.toJSONString(resp))
        );

        // 店铺信息
        Long shopId = searchParam.getShopId();
        if (shopId != null) {
            ShopInfo shop = merchantFeignClient.getShopInfoEntity(shopId);
            model.addAttribute(PageConstant.SHOP, shop);
        }

        // 搜索条件回显
        model.addAttribute("searchParam", searchParam);

        // 拼接urlParam
        String urlParam = makeUrlParam(searchParam);
        model.addAttribute("urlParam", urlParam);

        // 排序处理
        Map<String, String> orderMap = makeOrderMap(searchParam.getOrder());
        model.addAttribute("orderMap", orderMap);

        return "search/shop";
    }

    /**
     * 排序处理
     */
    private Map<String, String> makeOrderMap(String order) {
        HashMap<String, String> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)) {
            String[] split = order.split(":");
            if (split.length == 2) {
                map.put("type", split[0]);
                map.put("sort", split[1]);
                return map;
            }
        }
        map.put("type", "1");
        map.put("sort", "desc");
        return map;
    }

    /**
     * 面包屑——平台属性
     */
    private List<Map<String, String>> makePropsParamList(String[] props) {
        ArrayList<Map<String, String>> propsList = new ArrayList<>();
        if (props != null) {
            for (String prop : props) {
                // 23:4G:运行内存
                String[] split = prop.split(":");
                if (split.length == 3)
                    propsList.add(
                            new HashMap<String, String>() {{
                                put("attrId", split[0]);
                                put("attrName", split[2]);
                                put("attrValue", split[1]);

                            }}
                    );
            }
        }
        return propsList;
    }

    /**
     * 面包屑——品牌
     */
    private String makeUrlTradeMarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)) {
            // 2:华为
            String[] split = trademark.split(":");
            if (split.length == 2) return "品牌：" + split[1];
        }
        return "";
    }

    /**
     * 拼接urlParam
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder builder = new StringBuilder();

        // 店铺ID
        Long shopId = searchParam.getShopId();
        if (shopId != null) {
            builder.append("shopId=").append(shopId).append("&");
        }

        // 关键字
        if (!StringUtils.isEmpty(searchParam.getKeyword()))
            builder.append("keyword=").append(searchParam.getKeyword());

        // 分类
        if (searchParam.getCategory1Id() != null)
            builder.append("category1Id=").append(searchParam.getCategory1Id());
        if (searchParam.getCategory2Id() != null)
            builder.append("category2Id=").append(searchParam.getCategory2Id());
        if (searchParam.getCategory3Id() != null)
            builder.append("category3Id=").append(searchParam.getCategory3Id());

        // 品牌
        if (!StringUtils.isEmpty(searchParam.getTrademark()) && builder.length() > 0)
            builder.append("&trademark=").append(searchParam.getTrademark());

        // 平台属性
        String[] props = searchParam.getProps();
        if (props != null && props.length > 0 && builder.length() > 0) {
            for (String prop : props)
                builder.append("&props=").append(prop);
        }

        return (searchParam.getShopId() == null ? "search.html?" : "shop.html?") + builder;
    }
}
